//
// Created by work on 24-8-28.
//

#ifndef IOT_PLAT_CREDISDBMGR_H
#define IOT_PLAT_CREDISDBMGR_H
#include <os/miscellany/stddef.h>
#include <string>
#include <memory>
#include <unordered_map>
#include <unordered_set>

#include <iot_model_device/CModelInfo.h>
#include <iot_model_device/CUserInfo.h>
#include <iot_model_device/CRtDeviceInfo.h>

namespace iotplat {
    class CRedisDbMgr {
        STATIC_SINGLETON(CRedisDbMgr);
    public:
        static bool loadUser(std::string& userid);
        static bool unLoadUser(std::string& userid);
        static bool checkUserExist(std::string& userid);
        static bool getRtUser(const std::string& userid,  CUserInfo& user);

        static bool loadModel(const std::string& userid, const std::string& modelname);
        static bool getRtModel(std::string& userid, std::string& modelname, CModelInfo& model);
        static bool unLoadModel(std::string& userid, std::string& modelname);
        static bool getModelIndexs(const std::string& userid, std::unordered_set<std::string>& idxs);

        static bool loadModelYx(std::string& userid, std::string& modelname, std::string& yx);
        static bool getRtModelYx(std::string& userid, std::string& modelname, std::string& yxName,CRtModelYxInfo& yx);
        static bool unLoadModelYx(std::string& userid, std::string& modelname, std::string& yx);
        static bool getModelYxIndexs(const std::string& userid, const std::string& modelname, std::unordered_set<std::string>& idxs);

        static bool loadModelYc(const std::string &userid, const std::string &modelname, const std::string &yc);
        static bool getRtModelYc(std::string& userid, std::string& modelname, std::string& ycName,CRtModelYcInfo& yc);
        static bool unLoadModelYc(std::string& userid, std::string& modelname, std::string& yc);
        static bool getModelYcIndexs(const std::string& userid, const std::string& modelname, std::unordered_set<std::string>& idxs);

        static bool loadModelYk(const std::string& userid, const std::string& modelname, const std::string& yk);
        static bool getRtModelYk(std::string& userid, std::string& modelname, std::string& ykName,CRtModelYkInfo& yk);
        static bool unLoadModelYk(std::string& userid, std::string& modelname, std::string& yk);
        static bool getModelYkIndexs(const std::string& userid, const std::string& modelname, std::unordered_set<std::string>& idxs);

        static bool loadModelParameter(const std::string &userid, const std::string &modelname, const std::string &parameter);
        static bool getRtModelParameter(std::string& userid, std::string& modelname, std::string& parameterName,CRtModelParameterInfo& parameter);
        static bool unLoadModelParameter(std::string& userid, std::string& modelname, std::string& parameter);
        static bool getModelParameterIndexs(const std::string& userid, const std::string& modelname, std::unordered_set<std::string>& idxs);
        
        static bool loadDevice(const std::string &userid, const std::string &modelname, const std::string name);
        static bool getDeviceIndexs(const std::string& userid, const std::string& modelname, std::unordered_set<std::string>& idxs);
        static bool getRtDevice(const std::string &userid, const std::string &modelname, const std::string name, CRtDeviceInfo& device);
        static bool getRtDevices(const std::string& userid,const std::string& modelname, std::vector<CRtDeviceInfo>& devices);

        static bool reset();
    public:
        bool existsKey(std::string key);
        static bool getHashValueAsBool(const std::string& key, const std::string& field, bool &flag);
        static bool redisSetBoolFlag(const std::string& key, const std::string field, bool flag);

    protected:
        static bool redisAddSetValue(const std::string& key, const std::string& value);
        static bool redisRemoveSetValue(const std::string& key, const std::string& value);

        static bool redisRemoveSet(const std::string& key, const std::string& value);

        static bool redisAddHashMap(const std::string& key, std::unordered_map<std::string, std::string>& map);
        static bool redisRemoveHashMap(const std::string& key);

        static bool getHashAllValue(const std::string & key, std::unordered_map<std::string, std::string>& hashMap);
        static bool getSetAllValue(const std::string & key, std::unordered_set<std::string>& set);
    };
}


#endif //IOT_PLAT_CREDISDBMGR_H
